package tech.edwardvan.quartzdemo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tech.edwardvan.quartzdemo.common.ServerResponse;
import tech.edwardvan.quartzdemo.entity.Task;
import tech.edwardvan.quartzdemo.service.IScheduleService;

import java.util.List;

/**
 * @author EdwardVan
 */
@Api(tags = "调度")
@RestController
@RequestMapping("/schedules")
public class ScheduleController {

    @Autowired
    IScheduleService scheduleService;

    @ApiOperation("任务列表")
    @GetMapping
    public ServerResponse taskList() throws SchedulerException {
        List<Task> taskList = scheduleService.getTaskList();
        return ServerResponse.success(taskList);
    }

    @ApiOperation("任务详情")
    @GetMapping("/{taskId}")
    public ServerResponse taskByTaskName(@PathVariable("taskId") String taskId) throws SchedulerException {
        Task task = scheduleService.getTask(taskId);
        return ServerResponse.success(task);
    }

    @ApiOperation("新建并执行任务")
    @PostMapping
    public ServerResponse addTask(@RequestBody Task task) throws SchedulerException {
        scheduleService.addTask(task);
        return ServerResponse.successMessage("新增定时任务成功");
    }

    @ApiOperation("修改任务")
    @PostMapping("/{taskId}/update")
    public ServerResponse updateTask(@PathVariable("taskId") String taskId, @RequestBody Task task) throws SchedulerException {
        scheduleService.updateTask(task);
        return ServerResponse.successMessage("修改定时任务成功");
    }

    @ApiOperation("删除任务")
    @PostMapping("/{taskId}/delete")
    public ServerResponse deleteTask(@PathVariable("taskId") String taskId) throws SchedulerException {
        scheduleService.deleteTask(taskId);
        return ServerResponse.successMessage("删除定时任务成功");
    }

    @ApiOperation("暂停执行任务")
    @PostMapping("/{taskId}/pause")
    public ServerResponse pauseTask(@PathVariable("taskId") String taskId) throws SchedulerException {
        scheduleService.pauseTask(taskId);
        return ServerResponse.successMessage("暂停执行任务成功");
    }

    @ApiOperation("继续执行任务")
    @PostMapping("/{taskId}/resume")
    public ServerResponse resumeTask(@PathVariable("taskId") String taskId) throws SchedulerException {
        scheduleService.resumeTask(taskId);
        return ServerResponse.successMessage("继续执行任务成功");
    }

}
